public class Demo7 {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            Thread currentThread = Thread.currentThread();
            while(!currentThread.isInterrupted()){
                System.out.println("t线程正在运行");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    //执行保存文件等必要的操作
                    break;
                }
            }
        });
        t.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        t.interrupt();
    }
}
